/*
 *   This File is Part of CuteOgre Game Engine
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *	Copyright 2010 Alexandre Pires ( c.alexandre.pires@gmail.com )
 *  Created on: 2010/08/22
 */

#include "RenderManager.h"
#include "ConfigManager.h"
#include "LogManager.h"
#include "Ogg/OggUtils.h"

#include "OGRE/OgreWindowEventUtilities.h"

template<> RenderManager* Singleton<RenderManager>::ms_Singleton = 0;

void RenderManager::switchToMode ( RenderMode rMode, bool rSameCamera ) {

	if ( mRenderMode == rMode || mShutdown )
		return;

	Ogre::SceneManager* sceneManager;
	Ogre::Camera* camera;

	if ( rMode == RenderManager::ModeVideoPlayback || rMode
			== RenderManager::ModeIdle || rMode == RenderManager::ModeGui ) {

		LogManager::message ( "RenderManager::Switching to Generic Scene Mode" );

		if ( !mRoot->hasSceneManager ( "generic_scene" ) ) {

			LogManager::message (
					"RenderManager::Creating generic scene manager" );

			sceneManager = mRoot->createSceneManager ( Ogre::ST_GENERIC,
					"generic_scene" );
			camera = sceneManager->createCamera ( "generic_camera" );
			camera->setNearClipDistance ( 5 );

			int width = ConfigManager::readInt ( "width", 640 );
			int height = ConfigManager::readInt ( "height", 480 );

			camera->setAspectRatio ( (Ogre::Real) width / height );

		} else {

			LogManager::message (
					"RenderManager::Loading generic scene manager" );

			sceneManager = mRoot->getSceneManager ( "generic_scene" );
			camera = sceneManager->getCamera ( "generic_camera" );
		}

	} else {
		return;
	}

	LogManager::message ( "RenderManager::Setting up new mode" );

	mCurrentSceneMgr = sceneManager;

	if ( !rSameCamera || !mCurrentCamera ) {
		LogManager::message ( "RenderManager::Changing camera" );
		mCurrentCamera = camera;
	}

	mWindow->removeAllViewports ();

	Ogre::Viewport* viewport = mWindow->addViewport ( mCurrentCamera );
	viewport->setBackgroundColour ( Ogre::ColourValue ( 0, 0, 0 ) );

	mRenderMode = rMode;
}

void RenderManager::update () {

	if ( mShutdown )
		return;

	Ogre::WindowEventUtilities::messagePump ();
	mRoot->renderOneFrame ();
}

void RenderManager::init () {

	if ( !mShutdown )
		return;

	LogManager::message ( "RenderManager::Starting up" );

	LogManager::message ( "RenderManager::Creating YUV to RGB tables" );

	createYUVtoRGBtables ();

	LogManager::message ( "RenderManager::Starting up Ogre" );

	QString videoPlugin = ConfigManager::readString ( "video_plugin",
			"RenderSystem_GL" );
	QString deviceName = ConfigManager::readString ( "video_api",
			"OpenGL Rendering Subsystem" );

	mRoot = new Ogre::Root ( "", "" );
	mRoot->loadPlugin ( "Plugin_OctreeSceneManager" );
	mRoot->loadPlugin ( videoPlugin.toStdString () );

	Ogre::RenderSystemList::const_iterator pRend =
			mRoot->getAvailableRenderers ().begin ();

	while ( pRend != mRoot->getAvailableRenderers ().end () ) {
		Ogre::String rName = ( *pRend )->getName ();
		if ( rName == deviceName.toStdString () )
			break;
		pRend++;
	}

	Ogre::RenderSystem* rsys = *pRend;
	mRoot->setRenderSystem ( rsys );
	mRoot->initialise ( false );

	int width = ConfigManager::readInt ( "width", 640 );
	int height = ConfigManager::readInt ( "height", 480 );
	bool full = ConfigManager::readBool ( "fullscreen", false );

	mWindow = mRoot->createRenderWindow ( "Game Window", width, height, full );

	mCurrentSceneMgr = 0;
	mCurrentCamera = 0;

	switchToMode ( ModeIdle );

	mShutdown = false;
}

void RenderManager::shutdown () {

	if ( mShutdown )
		return;

	LogManager::message ( "RenderManager::Shutting down" );

	mShutdown = true;

	LogManager::message ( "RenderManager::Shutting down Ogre" );
	mRoot->shutdown ();

	LogManager::message ( "RenderManager::Deleting Ogre resources" );
	delete mRoot;
	delete mWindow;

	mRoot = 0;
	mWindow = 0;
}
